<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>Hello MUI</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<link rel="stylesheet" href="../css/mui.min.css">
		<link rel="stylesheet" href="../css/index/text3.css" />
		<style>
		</style>
	</head>

	<body>
		<div id="pullrefresh" class="mui-content">
			<div class="mui-content">
			  <div class="mui-row box-top" id="reback">
			        <div class="mui-col-sm-6 mui-col-xs-12 title-box">
			            <i class="mui-icon mui-icon-back"></i>
			        </div>
			        <div class="mui-col-sm-6 mui-col-xs-12">
			        </div>
			    </div>
			</div>
			<div>
            <div class="mui-content-padded box-list mui-scroll-wrapper">
            	<div>
	            	<ul class="mui-table-view mui-scroll" id="list1">
	                </ul>
	                <div id="demo1">
						<span>搜索中...</span>
					</div>
	            </div>
                
            </div>
			</div>
			<!--  -->
			<div class="caremam-btn-con">
				<div class="mui-row box-top" id="reback">
			        <div class="mui-col-sm-6 mui-col-xs-6">
			            <button type="button" class="mui-btn mui-btn-success" id="blool">打开蓝牙</button>
			        </div>
			        <div class="mui-col-sm-6 mui-col-xs-6">
			            <button type="button" class="mui-btn mui-btn-success" id="blools">蓝牙列表</button>
			        </div>
			 	</div>
			</div>
		</div>
	</body>
	<script src="../js/mui.min.js"></script>
	<script>
		mui.init();
		
		document.getElementById('reback').addEventListener('tap', function() {
				mui.back()
			})
		
	    mui('.mui-scroll-wrapper').scroll({
	    	 scrollY: true, //是否竖向滚动
			 scrollX: false, //是否横向滚动
			 startX: 0, //初始化时滚动至x
			 startY: 0, //初始化时滚动至y
			 indicators: false, //是否显示滚动条
			 deceleration:0.0006, //阻尼系数,系数越小滑动越灵敏
		})
        document.getElementById('blool').addEventListener('tap', function(){
        	var main, Context, BluetoothManager, BluetoothAdapter, BManager, BAdapter;
            main = plus.android.runtimeMainActivity();
			Context = plus.android.importClass("android.content.Context");
			BManager = main.getSystemService(Context.BLUETOOTH_SERVICE);
			plus.android.importClass(BManager);//引入相关的method函数
			BAdapter = BManager.getAdapter();
			plus.android.importClass(BAdapter);
            if(!BAdapter.isEnabled()) {
                BAdapter.enable();
            } else {
            	mui.alert('蓝牙已开启', '提示', ['确定'], null, 'div')
            }
        });
        
        document.getElementById('blools').addEventListener('tap', function () {
        	bluetooth_list()
        })
        // 蓝牙列表
        function bluetooth_list(){
        	 var ulList = '';
		    var main = plus.android.runtimeMainActivity();
		    var IntentFilter = plus.android.importClass('android.content.IntentFilter');
		    var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
		    var BluetoothDevice = plus.android.importClass("android.bluetooth.BluetoothDevice");
		    var BAdapter = BluetoothAdapter.getDefaultAdapter();
		    plus.android.importClass(BAdapter);
		   if(!BAdapter.isEnabled()) {
                mui.alert('请打开蓝牙', '提示', ['确定'], null, 'div')
                return
            }
		
		    var filter = new IntentFilter();
		    var bdevice = new BluetoothDevice();
		    var on = null;
		    var un = null;
		    var vlist1 = document.getElementById('list1'); //注册容器用来显示未配对设备
		    vlist1.innerHTML = ''; //清空容器
		    BAdapter.startDiscovery(); //开启搜索
		    var receiver;
		    document.getElementById('demo1').style.display= 'block'
		    receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
		        onReceive: function(context, intent) { //实现onReceiver回调函数
		            plus.android.importClass(intent); //通过intent实例引入intent类，方便以后的‘.’操作
		            if(intent.getAction() == "android.bluetooth.adapter.action.DISCOVERY_FINISHED") {
		                main.unregisterReceiver(receiver); //取消监听
		                document.getElementById('demo1').style.display= 'none'
		            } else if(intent.getAction() == "android.bluetooth.adapter.action.DISCOVERY_STARTED") {
		            } else {
		                BleDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
		                //判断是否配对
//		                console.log("蓝牙设备：" + BleDevice.getName() + '    ' + BleDevice.getAddress());
		                var li = document.createElement('li');
		                li.setAttribute('class', 'mui-table-view-cell');
		                var p1 = document.createElement("p");
		                p1.appendChild(document.createTextNode('名称：' + BleDevice.getName()));
		                var p2 = document.createElement("p");
		                p2.appendChild(document.createTextNode('地址：' + BleDevice.getAddress()));
		                li.appendChild(p1);
		                li.appendChild(p2);
		                li.setAttribute('id', BleDevice.getName())
		                li.setAttribute('onclick', 'searchDevices(id)')
		                vlist1.appendChild(li);
		            }
		        }
		    });
		
		    filter.addAction(bdevice.ACTION_FOUND);
		    filter.addAction(BAdapter.ACTION_DISCOVERY_STARTED);
		    filter.addAction(BAdapter.ACTION_DISCOVERY_FINISHED);
		    filter.addAction(BAdapter.ACTION_STATE_CHANGED);
		
		    main.registerReceiver(receiver, filter); //注册监听
		}
        function searchDevices(id) {
        	//注册类
//		    var main = plus.android.runtimeMainActivity();
//		    var IntentFilter = plus.android.importClass('android.content.IntentFilter');
//		    var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
//		    var BluetoothDevice = plus.android.importClass("android.bluetooth.BluetoothDevice");
//		    console.log("开始搜索设备");
//		    var bdevice = new BluetoothDevice();
//		    console.log(bdevice)
//		    var receiver;
//		    receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
//		        onReceive: function(context, intent) { //实现onReceiver回调函数
//		            plus.android.importClass(intent); //通过intent实例引入intent类，方便以后的‘.’操作
//		            console.log(intent.getAction()); //获取action
//		            BleDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//		            //判断是否配对
//		            if (BleDevice.getBondState() == bdevice.BOND_NONE) {
//		                console.log("未配对蓝牙设备：" + BleDevice.getName() + '    ' + BleDevice.getAddress());
//		                //参数如果跟取得的mac地址一样就配对
//		                if (address == BleDevice.getAddress()) {
//		                    if (BleDevice.createBond()) { //配对命令.createBond()
//		                        console.log("配对成功");
//		                        var li2 = document.createElement('li'); //注册
//		                        li2.setAttribute('id', BleDevice.getAddress()); //打印机mac地址
//		                        li2.setAttribute('onclick', 'print(id)'); //注册click点击列表进行打印
//		                        li2.innerText = BleDevice.getName();
//		                        vlist2.appendChild(li2);
//		                    }
//		                } 
//		            }
//		        }
//	        }
		}
        
	</script>
</html>